package com.travel.group.controller;

import  com.travel.group.common.PageResult;
import com.travel.group.entity.WxUsers;
import com.travel.group.service.WxUserService;
import com.travel.group.utils.WeChatResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/wx")
public class WxUserController {

    @Autowired
    private WxUserService wxUserService;

    @RequestMapping("/toPage")
    public String toPage(){
        return "wxuser/list";
    }

    @RequestMapping("/storageInfo/user")
    @ResponseBody
    public Object WxRegisterUser(@RequestBody WxUsers wxUser){
        WxUsers dbWxUser = wxUserService.getWxUserByUserName(wxUser.getUsername());
        if (dbWxUser == null){ //保证用户名不能重复
            String userId = wxUserService.registerWxUser(wxUser);
            return WeChatResponseUtil.ok("userId",userId);
        }
        else
            return WeChatResponseUtil.fail();
    }

    @RequestMapping("/getWxUserById/user")
    @ResponseBody
    public Object getWxUserById(HttpServletRequest request){
        String userId = request.getParameter("userId");
        WxUsers wxUsers = wxUserService.getById(userId);
        return WeChatResponseUtil.ok("wxUser",wxUsers);
    }

    /**
     * 查询所有微信用户
     * @param page 起始页数
     * @param limit 限制每页多少条数据
     * @return
     */
    @RequestMapping("/getAllWxUsers")
    @ResponseBody
    public PageResult<WxUsers> list(Integer page, Integer limit) {
        if (page == null) {
            page = 0;
        }
        if (limit == null) {
            limit = 10;
        }
        List<WxUsers> allArticle = wxUserService.getAllWxUser(page, limit);
        Integer count = wxUserService.countData();
        return new PageResult<>(allArticle, count);
    }

    @RequestMapping(value = "/wxUser/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public PageResult delete(@PathVariable("id") String id){
        if(wxUserService.removeById(id)){
            return new PageResult("删除成功",200);
        }
        return new PageResult("删除失败",400);
    }

    /**
     * 用户登录时判断
     * @param request
     * @return
     */
    @RequestMapping("/checkWxUser/user")
    @ResponseBody
    public Object checkWxUser(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        WxUsers wxUsers = wxUserService.checkWxUser(username, password);
        if (wxUsers != null){
            HttpSession session = request.getSession();
            session.setAttribute("wxUser", wxUsers);
            return WeChatResponseUtil.ok("userId",wxUsers.getUserId());
        }
        else
            return WeChatResponseUtil.fail();
    }

    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public Object updateUserInfo(@RequestBody WxUsers wxUser){
        if(wxUserService.updateById(wxUser))
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }

    @RequestMapping("/revisePwd/user")
    @ResponseBody
    public Object revisePwd(HttpServletRequest request){
        String userId = request.getParameter("userid");
        String oldPwd = request.getParameter("oldPwd");
        String newPwd = request.getParameter("newPwd");
        WxUsers wxUsers = wxUserService.getById(userId);
        if (!wxUsers.getPassword().equals(oldPwd))
            return WeChatResponseUtil.fail();
        else {
            wxUsers.setPassword(newPwd);
            wxUserService.updateById(wxUsers);
            return WeChatResponseUtil.ok();
        }
    }
}
